package com.ruoyi.web.controller.activity.admin;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.activity.service.IActivityService;
import com.ruoyi.activity.service.IContestService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.model.activity.Activity;
import com.ruoyi.model.apply.excel.ExcelApplyVO;
import com.ruoyi.model.message.UserMessage;
import com.ruoyi.model.template.TemplateValue;
import com.ruoyi.model.template.vo.TemplateVo;
import com.ruoyi.ucenter.service.IUserMessageService;
import com.ruoyi.web.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 张怀秋
 **/
@Api(tags = "后台:活动接口")
@RestController
@RequestMapping("/admin/activity")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ActivityAdminController extends BaseController {

    /**
     * 活动服务
     */
    private final IActivityService activityService;

    /**
     * 消息服务
     */
    private final IUserMessageService messageService;


    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:activity:list')")
    @GetMapping("/list")
    @ApiOperation("活动列表")
    public TableDataInfo list(Activity activity) {
        startPage();
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity) {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<>(Activity.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(activityService.selectActivityById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('system:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity) {
        return toAjax(activityService.insertActivity(activity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('system:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity) {
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('system:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(activityService.deleteActivityByIds(ids));
    }

    @ApiOperation("审核竞赛信息")
    @GetMapping("/check/activity/{activityId}/{status}")
    public AjaxResult checkContest(@ApiParam("当前审核的竞赛id") @PathVariable("activityId") String activityId,
                                   @ApiParam("当前的审核状态") @PathVariable("status") String status) {
        return toAjax(this.activityService.checkActivityStatus(activityId, status));
    }

    @ApiOperation("审核完成后编辑消息")
    @PostMapping("/send/message")
    public AjaxResult sendMessage(@ApiParam("发送的消息") @RequestBody UserMessage userMessage) {
        return toAjax(this.messageService.insertUserMessage(userMessage));
    }
}
